package com.gc.server.controller.api;

import com.gc.server.entity.PlanEntity;
import com.gc.server.entity.TodoEntity;
import com.gc.server.service.PlanService;
import com.gc.server.service.TodoService;
import com.yf.annotation.IgnoreAuth;
import com.yf.annotation.LoginUserId;
import com.yf.response.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RequestMapping("/api/plan")
@RestController
public class ApiPlanController {

    @Autowired
    private PlanService planService;

    @Autowired
    private TodoService todoService;

    /**
     * 添加周计划
     * @param userId
     * @param startDate
     * @return
     */
    @PostMapping("/add")
    public R add(@LoginUserId Long userId, Date startDate,Date endDate,int year,int week) {
       PlanEntity planEntity = planService.add(userId,startDate,endDate,year,week);
        return R.ok(planEntity);
    }

    //按年查找
    @GetMapping("/year/{year}")
    public R listByYear(@PathVariable Integer year,@LoginUserId Long userId){
        List<PlanEntity> list = planService.findByYear(userId,year);
        return R.ok(list);
    }

    /**
     * 获取周计划列表
     * @param userId
     * @return
     */
    @GetMapping("/list")
    public R list(@LoginUserId Long userId){
        return R.ok(planService.list(userId));
    }

    /**
     * 获取单个计划信息
     * @param userId
     * @param planId
     * @return
     */
    @GetMapping("/{planId}")
    public R get(@LoginUserId Long userId,@PathVariable Long planId){
        PlanEntity planEntity = planService.get(planId);
        List<TodoEntity> todoEntities = todoService.findByPlanId(planId);
        planEntity.setItems(todoEntities);
        return R.ok(planEntity);
    }






}
